/**
 * @file categories/graph/floyd/reconstruction/main.cpp
 * @brief
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/7/6 21:44:38
 **/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int N = 205;
int n, m;
int a[N], f[N][N];  // 邻接矩阵存边
void update(int k) {
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (f[i][j] > f[i][k] + f[j][k]) f[i][j] = f[j][i] = f[i][k] + f[j][k];
}
int main() {
  // High rating and good luck!
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> a[i];
  memset(f, 0x3f, sizeof f);
  for (int i = 0; i < n; ++i) f[i][i] = 0;
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    f[u][v] = f[v][u] = w;
  }
  int q;
  cin >> q;
  int now = 0;
  for (int i = 1; i <= q; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    while (a[now] <= w && now < n) {
      update(now);
      now++;
    }
    if (a[u] > w || a[v] > w)
      cout << -1 << endl;
    else {
      if (f[u][v] == 0x3f3f3f3f)
        cout << -1 << endl;
      else
        cout << f[u][v] << endl;
    }
  }
  return 0;
}
